package com.hjh.thymeleafdemo1.web.component;

import com.hjh.thymeleafdemo1.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器 , 用于对登录进行检查，没有登录的话，是不能对指定的页面进行访问。
 */
public class LoginInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception{
        User user = (User) request.getSession().getAttribute("loginUser");
        if (user == null){
            request.setAttribute("msg","没有权限请先登录!");
            response.sendRedirect("/toLogin");
            return false;
        }else {
            return true;
        }
    }
}
